//
//  MyViewController.m
//  bh
//
//  Created by lanou on 15/12/24.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MyViewController.h"
#import <CoreImage/CoreImage.h>
#import <AVFoundation/AVFoundation.h>
@interface MyViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property(nonatomic,strong)AVCaptureSession *session;


@property(nonatomic,strong)AVCaptureVideoPreviewLayer *preViewLayer ;

@property(nonatomic,strong)UILabel *label;
@end

@implementation MyViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
    self.label.font = [UIFont systemFontOfSize:20];
    self.view.backgroundColor = [UIColor whiteColor];
    
       [self.view addSubview:self.label];
    
    
    //    1、实例化拍摄设备AVCaptureDevice（AVMediaTypeVideo）
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //    2，创建输入源：AVCaptureDeviceInput并与拍摄设备关联起来
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    //
    //    3，创建输出源：AVCaptureMetadataOutput并设置代理
    //
    AVCaptureMetadataOutput *outPut = [[AVCaptureMetadataOutput alloc]init];
    
    [outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //    4，创建会话：AVCaptureSession并将输入源输出源关联起来
    
    self.session = [[AVCaptureSession alloc]init];
    
    [_session addInput:input];
    [_session addOutput:outPut];
    
    //    5，输出源设置输出数据类型：setMetadataObjectTypes：
    //    类型：AVMetadataObjectTypeQRCode
    //
    [outPut setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //    6，实例化预览图层：AVCaptureVideoPreviewLayer 并添加
    //
    _preViewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:_session];
    _preViewLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:_preViewLayer];
    //    7，会话开始执行
    //
    
    [_session startRunning];
    //    8，实现输出源的代理方法
    
    // Do any additional setup after loading the view.
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    [self.session stopRunning];
    [self.preViewLayer removeFromSuperlayer];
    if (metadataObjects.count > 0) {
        NSString *string = [[metadataObjects lastObject] stringValue];
 
        
        UIWebView *web = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
        [self.view addSubview:web];
        

        self.label.text = string;
    }
}


- (void)bornQRCode
{
    //    1， 实例化二维码滤镜(CIQRCodeGenerator)
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    NSString *string = @"哈哈哈哈哈哈";
    
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    //      2，通过KVO设置滤镜传入数据(inputMessage)
    
    [filter setValue:data forKey:@"inputMessage"];
    
    //    3，生成二维码
    
    CIImage *iconImage =  [filter outputImage];
    
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    UIImage *image = [UIImage imageWithCIImage:iconImage];
    
    tempImageView.image = image;
    
    [self.view addSubview:tempImageView];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
